package com.test;

import java.util.ArrayList;

public class ArrayListTest4 {
    public static void main(String[] args) {
        //定义一个集合，添加一些学生对象，并进行遍历
        //1.创建集合
        ArrayList<Student> list = new ArrayList<>();
        //2.创建学生对象
        Student student1 = new Student("zhangshan",18);
        Student student2 = new Student("wangwu",19);
        Student student3 = new Student("lishi",20);
        Student student4 = new Student("zhangqi",21);
        //3.添加元素
        list.add(student1);
        list.add(student2);
        list.add(student3);
        list.add(student4);
        //4.遍历集合
        for (int i = 0; i < list.size(); i++) {
            //i 索引
            //list.get(i)   元素
            Student student = list.get(i);
            System.out.println("姓名为:"+student.getName()+",年龄为:"+student.getAge());
        }
    }
}
